package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.dao.business.domain.Feedback;
import org.dromara.dao.business.domain.FeedbackQuery;
import org.dromara.dao.business.mapper.FeedbackMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
public class FeedbackService {

    @Autowired
    FeedbackMapper feedbackMapper;


    public TableDataInfo<Feedback> queryPageList(FeedbackQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                feedbackMapper.paginationSelectList(query, pageQuery),
                feedbackMapper.paginationSelectCount(query)
        );
    }


    public Feedback queryById(Long id) {
        return feedbackMapper.selectById(id);
    }


    @Transactional
    public Feedback insert(Feedback entity) {
        feedbackMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(Feedback update) {

        Feedback entity = feedbackMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setUserId(update.getUserId());
        entity.setUsername(update.getUsername());
        entity.setContent(update.getContent());
        entity.setImages(update.getImages());
        entity.setEmail(update.getEmail());
        entity.setUpdateTime(LocalDateTime.now());
        feedbackMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        feedbackMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        feedbackMapper.deleteByIds(Arrays.asList(ids));
    }



    @Transactional
    public void commitFeedback(String content, String images, String email) {
        LoginUser loginUser = LoginHelper.getAppLoginUser();
        Feedback feedback = new Feedback();
        feedback.setUserId(loginUser.getUserId());
        feedback.setUsername(loginUser.getUsername());
        feedback.setContent(content);
        feedback.setImages(images);
        feedback.setEmail(email);

        feedbackMapper.insert(feedback);
    }
}
